package com.zyl.util;

//	public static final String DATABASE_NAME = "points.db";//数据库名
//	public static final int DATABASE_VERSION = 2;				 //版本
//	public static final String TASK_TABLE = "task";	 //表名


import java.io.IOException;

import com.zyl.bean.Note;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	public static final String DATABASE_NAME = "speednote.db";//数据库名
	public static final int DATABASE_VERSION = 1;				 //版本
	public DBHelper(Context context)
	{
		super(context, DATABASE_NAME,null, DATABASE_VERSION);
	}
	
	
	 
	@Override
	public void onCreate(SQLiteDatabase db) {
		//Debug.startMethodTracing("DBHelperActivityTrace");
		String sql="Create table task(id integer primary key autoincrement,  qid integer ,   page integer ,   size integer ,appearTimes integer,right boolean, rightIndex integer, totalCount integer);";
		db.execSQL(sql); 
		
		sql="Create table note(id integer primary key autoincrement, note text , date integer );";
		db.execSQL(sql); 
		sql="Create table bill(id integer primary key autoincrement, note integer , date integer );";
		db.execSQL(sql); 
		sql="Create table plan(id integer primary key autoincrement, note text , date integer );";
		db.execSQL(sql); 
		sql="Create table 'case'(id integer primary key autoincrement, note text , date integer );";
		db.execSQL(sql); 
		
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String sql=" DROP TABLE IF EXISTS task ";
		db.execSQL(sql);
		sql=" DROP TABLE IF EXISTS note ";
		db.execSQL(sql);
		sql=" DROP TABLE IF EXISTS bill ";
		db.execSQL(sql);
		sql=" DROP TABLE IF EXISTS plan ";
		db.execSQL(sql);
		sql=" DROP TABLE IF EXISTS case ";
		db.execSQL(sql);
		onCreate(db);
	}

	 public Integer getCount() {  
         SQLiteDatabase db = this.getReadableDatabase();  
         Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM note", null);  
         cursor.moveToNext(); 
         int count =  cursor.getInt(0);
 		 cursor.close();
         db.close();  
         return count;  
     }  
	 
	 public void insert(Note n) {  
		 try {
			FileUtil.write(n.toString());
		} catch (IOException e) {
		}
         SQLiteDatabase db = this.getWritableDatabase();  //获取到数据库  
         db.execSQL("INSERT INTO note(date,note) VALUES(?,?)", new Object[] { n.getDate(), n.getNote()});  
         db.close();  
     }
	 

	
}
